<template>
  <view class="company-container">
    <view class="form-content">
		<view class="form-title">
			<view class="line"></view>公司信息
		</view>
      <up-form
        labelPosition="left"
        :model="companyForm"
        ref="companyFormRef"
        labelWidth="110px"
      >
        <up-form-item label="公司名称" prop="name" borderBottom required>
          <up-input
            v-model="companyForm.name"
            placeholder="请输入公司名称"
            border="none"
          ></up-input>
        </up-form-item>
        <up-form-item label="公司简称" prop="shortName" borderBottom>
          <up-input
            v-model="companyForm.shortName"
            placeholder="请输入简称"
            border="none"
          ></up-input>
        </up-form-item>
        <up-form-item
          label="统一社会信用代码"
          prop="creditCode"
          borderBottom
        >
          <up-input
            v-model="companyForm.creditCode"
            placeholder="请输入社会信用代码"
            border="none"
          ></up-input>
        </up-form-item>
        <up-form-item label="公司地址" prop="adress" borderBottom>
          <up-textarea
            v-model="companyForm.adress"
            autoHeight
            placeholder="请输入公司地址"
            border="none"
          ></up-textarea>
        </up-form-item>
        <up-form-item label="公司邮箱" prop="email" borderBottom>
          <up-input
            v-model="companyForm.email"
            placeholder="请输入公司邮箱"
            border="none"
          ></up-input>
        </up-form-item>
      </up-form>
      <view class="bottom-btn">
        <up-button
          type="primary"
          color="#246eff"
          shape="circle"
          @click="handleSubmit"
          >保存</up-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { addOrSaveCompany,getCompanyInfo } from '@/api/company';
export default {
  data() {
    return {
      dialogType: 'add',
      companyForm: {
        name: '',
        shortName: '',
        creditCode: '',
        adress: '',
        email: '',
        logoUrl: '',
      },
      rules: {
        name: [
          {
            required: true,
            message: '请填写公司名称',
            trigger: ['blur', 'change'],
          },
        ]
      },
    };
  },
  onLoad(options) {
    this.initData(options);
  },
  mounted() {
    this.$refs.companyFormRef.setRules(this.rules);
  },
  methods: {
    initData(options) {
      const { type, companyId } = options;
      this.dialogType = type;
      uni.setNavigationBarTitle({
        title: type == 'add' ? '添加公司' : '公司详情',
      });
      if (type !== 'add') {
		getCompanyInfo(companyId).then(res=>{
			this.companyForm = res;
		})
        
      }
    },
    handleSubmit() {
      this.$refs.companyFormRef
        .validate()
        .then((valid) => {
          if (valid) {
            const params = this.companyForm;
            addOrSaveCompany(params).then((res) => {
              uni.$u.toast(this.dialogType == 'add' ? '添加成功' : '保存成功');
              setTimeout(() => {
				getApp().globalData.companyInfo = res;
				if(this.dialogType == 'add'){
					getApp().globalData.energyStationInfo={};
				}
                uni.switchTab({
                  url: '/pages/index/index',
                });
              }, 1000);
            });
          } else {
            uni.$u.toast('校验失败');
          }
        })
        .catch(() => {
          // 处理验证错误
          uni.$u.toast('校验失败');
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.company-container {
  width: 100%;
  min-height: 100vh;
  padding: 30rpx 20rpx;
  box-sizing: border-box;
  position: relative;
  background: #f8f8f8;
}

.form-content {
  padding: 20rpx 20rpx;
  width: 100%;
  box-sizing: border-box;
  background: #fff;
  border-radius: 12rpx;
  .form-title {
  	font-size: 34rpx;
  	font-weight: bold;
  	text-indent: 10rpx;
  	margin-bottom: 20rpx;
  	.line {
  		width: 5px;
  		height: 15px;
  		border-radius: 10px;
  		display: inline-block;
  		margin-right: 20rpx;
  		vertical-align: -2rpx;
  		background-color: #0e8fe5;
  	}
  }
}

.bottom-btn {
  margin-top: 40rpx;
  width: 100%;
}
</style>
